package com.kuang.controller;

import com.kuang.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import com.kuang.service.StudentService;
import com.kuang.pojo.Students;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;

    @RequestMapping("/allStudent")
    public String list(Model model){
        List<Students> list = studentService.queryAllStudent();
        model.addAttribute("list", list);
        return "allStudent";
    }

    @RequestMapping("/toAddStudent")
    public String toAddStudent() {return "addStudent";}

    @RequestMapping("/addStudent")
    public String addStudent(Students students){
        System.out.println(students);
        studentService.addStudent(students);
        return "redirect:/student/allStudent";
    }

    @RequestMapping("/toUpdateStudent")
    public String toUpdateStudent(Model model, String Sno)
    {
        Students students = studentService.queryStudentBySno(Sno);
        System.out.println(students);
        model.addAttribute("student", students);
        return "updateStudent";
    }

    @RequestMapping("/updateStudent")
    public String updateStudent(Model model, Students student){
        System.out.println(student);
        studentService.updateStudent(student);
        Students students = studentService.queryStudentBySno(student.getSno());
        model.addAttribute("students", students);
        return "redirect:/student/allStudent";
    }

    @RequestMapping("/del/{studentSno}")
    public String deleteStudent(@PathVariable("studentSno") String Sno) {
        studentService.deleteStudentBySno(Sno);
        return "redirect:/student/allStudent";
    }

    @RequestMapping("/studentIndex")
    public String studentIndex(Model model, String studentSno){
        Students student = studentService.queryStudentBySno(studentSno);



        model.addAttribute("student", student);

        return "studentIndex";
    }

}
